<template>
  <el-dialog
    title="商家审核"
    v-model="visible"
    width="30%"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="审核结果">
        <el-radio-group v-model="form.isAudit">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">拒绝</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核备注" v-if="form.isAudit === 2">
        <el-input
          type="textarea"
          v-model="form.auditRemarks"
          :rows="3"
          placeholder="请输入拒绝理由"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { api } from "../../../api/index";

export default {
  name: "AuditDialog",
  props: {
    modelValue: {
      type: Boolean,
      default: false,
    },
    merchantData: {
      type: Object,
      default: () => ({}),
    },
  },
  emits: ["update:modelValue", "success"],
  data() {
    return {
      form: {
        id: null,
        isAudit: 1,
        auditRemarks: "",
      },
    };
  },
  computed: {
    visible: {
      get() {
        return this.modelValue;
      },
      set(val) {
        this.$emit("update:modelValue", val);
      },
    },
  },
  watch: {
    merchantData: {
      handler(val) {
        if (val && val.id) {
          this.form = {
            id: val.id,
            isAudit: 1,
            auditRemarks: "",
          };
        }
      },
      immediate: true,
    },
  },
  methods: {
    handleClose() {
      this.visible = false;
      this.form = {
        id: null,
        isAudit: 1,
        auditRemarks: "",
      };
    },
    handleConfirm() {
      if (this.form.isAudit === 2 && !this.form.auditRemarks) {
        this.$message.warning("请输入拒绝理由");
        return;
      }

      const params = {
        id: this.form.id,
        isAudit: this.form.isAudit,
      };

      if (this.form.isAudit === 2) {
        params.auditRemarks = this.form.auditRemarks;
      }

      api("/web/subscribersMerchant/verify", params).then((res) => {
        if (res.code === 2000) {
          this.$message.success("审核成功");
          this.handleClose();
          this.$emit("success");
        } else {
          this.$message.error(res.message);
        }
      });
    },
  },
};
</script>
